//
//  ActionTableViewCell.m
//  BaseFrame
//
//  Created by 孟学浩 on 2017/4/6.
//  Copyright © 2017年 com.mxh. All rights reserved.
//

#import "ActionTableViewCell.h"

@interface ActionTableViewCell()

@property (nonatomic,strong) UIImageView *imgView;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UILabel *buyLabel;
@property (nonatomic,strong) UIButton *joinBuyCarBtn;

@end


@implementation ActionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = 0;
        self.separatorInset = UIEdgeInsetsZero;
        if ([self respondsToSelector:@selector(setLayoutMargins:)]) {
            self.layoutMargins = UIEdgeInsetsZero;
        }
        _imgView = [UIImageView new];
        _imgView.contentMode = UIViewContentModeScaleAspectFill;
        _imgView.layer.masksToBounds = YES;
        _imgView.backgroundColor = getColor(@"f1f2f6");
        
        _infoLabel = [UILabel new];
        _infoLabel.numberOfLines = 0;
        _infoLabel.font = DEF_FontSize_12;
        
        _buyLabel = [UILabel new];
        _buyLabel.numberOfLines = 2;
        _buyLabel.text = @"立即\n购买";
        _buyLabel.textColor = [UIColor whiteColor];
        _buyLabel.backgroundColor = getColor(mainColor);
        _buyLabel.layer.masksToBounds = YES;
        _buyLabel.layer.cornerRadius = 8;
        _buyLabel.font = [UIFont systemFontOfSize:12 weight:1];
        _buyLabel.textAlignment = NSTextAlignmentCenter;
        
        _joinBuyCarBtn = [UIButton new];
        [_joinBuyCarBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_joinBuyCarBtn setTitleColor:getColor(mainColor) forState:UIControlStateNormal];
        _joinBuyCarBtn.titleLabel.font = DEF_FontSize_12;
        [_joinBuyCarBtn addTarget:self action:@selector(joinBuyCarEvent) forControlEvents:UIControlEventTouchUpInside];
        
        [self.contentView addSubview:_imgView];
        [self.contentView addSubview:_infoLabel];
        [self.contentView addSubview:_buyLabel];
        [self.contentView addSubview:_joinBuyCarBtn];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    _imgView.frame = CGRectMake(15, 0, self.frame.size.height-15, self.frame.size.height-15);
    _imgView.center = CGPointMake(_imgView.center.x, self.frame.size.height/2.);
    
    _infoLabel.frame = CGRectMake(CGRectGetMaxX(_imgView.frame)+5, 0, SCREEN_WIDTH - CGRectGetMaxX(_imgView.frame) - 60, self.frame.size.height);
    _buyLabel.frame = CGRectMake(SCREEN_WIDTH-60, 0, 40, 40);
    _buyLabel.center = CGPointMake(_buyLabel.center.x, self.frame.size.height/2.-8);
    
    _joinBuyCarBtn.frame = CGRectMake( 0, 0, 90, 30);
    _joinBuyCarBtn.center = CGPointMake(_buyLabel.center.x, _buyLabel.center.y+30);
}

- (void)joinBuyCarEvent{
    if (_getAddToCartClick) {
        _getAddToCartClick();
    }
    NSLog(@"加入购物车");
}

- (void)setModel:(GoodModel *)model{
    _model = model;
    [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:_model.gPicture] placeholderImage:[UIImage imageNamed:@"image"]];
    
    NSString *title = _model.gName;
    NSString *info = _model.gProperty;
    NSString *money = [NSString stringWithFormat:@"历史最低价      ¥%@",_model.gPrice];
    NSString *temp = [NSString stringWithFormat:@"%@\n%@\n%@",title,info,money];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:temp];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"545454") range:[temp rangeOfString:info]];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(redColor) range:[temp rangeOfString:money]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];//调整行间距
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [temp length])];
    
    _infoLabel.attributedText = attStr;
}

- (void)setDataDictionary:(NSDictionary *)dataDictionary{
    _dataDictionary = dataDictionary;
    [_imgView sd_setImageWithURL:[NSURL URLOffsetIPWithString:dataDictionary[@"c_first_figure"]]];
    
    NSString *title = dataDictionary[@"c_name"];
    NSString *info = [NSString stringWithFormat:@"介绍 %@",dataDictionary[@"c_introduce"]];
    NSString *money = [NSString stringWithFormat:@"价格 ¥%@",dataDictionary[@"c_unit_price"]];
    NSString *temp = [NSString stringWithFormat:@"%@\n%@\n%@",title,info,money];
    
    NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:temp];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(@"545454") range:[temp rangeOfString:info]];
    [attStr addAttribute:NSForegroundColorAttributeName value:getColor(redColor) range:[temp rangeOfString:money]];
    
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:3];//调整行间距
    [attStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [temp length])];
    
    _infoLabel.attributedText = attStr;
    
}

@end
